package com.hx.vending.service.impl;

import com.hx.vending.bean.Menu;
import com.hx.vending.bean.entity.MenuVO;
import com.hx.vending.dao.MenuMapper;
import com.hx.vending.service.IMenuService;
import com.hx.vending.util.Msg;
import com.hx.vending.util.ToolUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/1/2.
 */
@Service
public class MenuServiceImpl implements IMenuService {
    @Resource
    private MenuMapper menuMapper;

    @Override
    public int insertMenu(Menu menu) {
        return menuMapper.insertMenu(menu);
    }

    @Override
    public int updateByPrimaryKey(Menu menu) {
        return menuMapper.updateByPrimaryKey(menu);
    }

    @Override
    public List<Menu> selectByExample(Menu menu) {
        return menuMapper.selectByExample(menu);
    }

    @Override
    public Msg delMenuContainSubMenus(String menuIds) {
        if (menuIds.contains("-")){
            String[] strs = menuIds.split("-");
            for (int j = 0;j < strs.length;j++){
                Integer menuId = Integer.parseInt(strs[j]);
                Menu m = new Menu();
                m.setMenuId(menuId);
                Menu menu = menuMapper.selectByPrimaryKey(m);
                Integer result = delMenu(menuId);
                if(result>0){
                    //删除所有子菜单
                    Integer pid = menu.getMenuId();
                    Menu m1 = new Menu();
                    m1.setMenuSuperiorId(pid);
                    List<Menu> mm = menuMapper.selectByExample(m1);
                    if(ToolUtil.isNotEmpty(mm) && mm.size()>0){
                        for(int i = 0;i < mm.size();i++){
                            delMenu(mm.get(i).getMenuId());
                        }
                    }
                }
            }
            return Msg.success();
        }else{
           Integer menuId = Integer.parseInt(menuIds);
            Menu m = new Menu();
            m.setMenuId(menuId);
            Menu menu = menuMapper.selectByPrimaryKey(m);
            Integer result = delMenu(menuId);
            if(result>0){
                //删除所有子菜单
                Integer pid = menu.getMenuId();
                Menu m1 = new Menu();
                m1.setMenuSuperiorId(pid);
                List<Menu> mm = menuMapper.selectByExample(m1);
                if(ToolUtil.isNotEmpty(mm) && mm.size()>0){
                    for(int i = 0;i < mm.size();i++){
                         delMenu(mm.get(i).getMenuId());
                    }
                }
                return Msg.success();
            }
        }

       return Msg.fail();
    }

    @Override
    public Integer delMenu(Integer menuId) {

        //删除菜单
        int i = menuMapper.deleteByPrimaryKey(menuId);
        if(i > 0){
            //删除关联
            menuMapper.deleteRoleMenuByMenu(menuId);
        }
        return i;
    }

    @Override
    public Menu selectByPrimaryKey(Menu menu) {
        return menuMapper.selectByPrimaryKey(menu);
    }

    @Override
    public List<MenuVO> selectVoByExample(String menuName,Integer[] menus) {
        return menuMapper.selectVoByExample(menuName,menus);
    }

    @Override
    public Integer[] selectMenuByRole(Integer[] roleList) {
        return menuMapper.selectMenuByRole(roleList);
    }

    @Override
    public Integer[] selectMenu() {
        return menuMapper.selectMenu();
    }
}
